package com.betterjr.modules.risk.api.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.web.AbstarctRemoteApiInvoke;
import com.betterjr.common.web.AjaxRemoteApiParam;
import com.betterjr.modules.agreement.IScfElecAgreementService;

/**
 * 
 * @ClassName: RemoteApiQueryElecAgreementInvokeService 
 * @Description: 查询电子合同
 * @author xuyp
 * @date 2018年7月7日 下午4:12:49 
 *
 */
@Service
public class RemoteApiQueryElecAgreementInvokeService extends AbstarctRemoteApiInvoke {

    @Reference(interfaceClass = IScfElecAgreementService.class)
    private IScfElecAgreementService scfElecAgreementService;

    private String apiCode = "RAI00007";

    @Override
    public String saveOrFindInvoke(AjaxRemoteApiParam param) {

        return scfElecAgreementService.webQueryElecAgreementByPage(convertToUseMap(param.getParamMap()),
                findProperties(param.getParamMap(), "pageNum", Integer.class),
                findProperties(param.getParamMap(), "pageSize", Integer.class));
    }

    @Override
    public boolean supportInvoke(String anApiCode) {

        return apiCode.equals(anApiCode);
    }

    @Override
    public boolean checkParam(Map<String, Object> anParamMap) {

        boolean flag = true;

        if (flag && Collections3.isEmpty(anParamMap)) flag = false;

        if (flag && !anParamMap.containsKey("GTEregDate")) flag = false;
        if (flag && !anParamMap.containsKey("LTEregDate")) flag = false;
        if (flag && !anParamMap.containsKey("signStatus")) flag = false;
        if (flag && !anParamMap.containsKey("custNo")) flag = false;
        if (flag && !anParamMap.containsKey("flag")) flag = false;
        if (flag && !anParamMap.containsKey("pageNum")) flag = false;
        if (flag && !anParamMap.containsKey("pageSize")) flag = false;

        return flag;

    }

    private Map<String, Object> convertToUseMap(Map<String, Object> sourceMap) {

        Map<String, Object> map = new HashMap<>(sourceMap.size());

        map.put("custNo", findProperties(sourceMap, "custNo", Long.class));
        map.put("GTEregDate", findProperties(sourceMap, "GTEregDate").replaceAll("-", ""));
        map.put("LTEregDate", findProperties(sourceMap, "LTEregDate").replaceAll("-", ""));
        map.put("signStatus", findProperties(sourceMap, "signStatus"));
        map.put("flag", findProperties(sourceMap, "flag"));

        map.put("orgRegType", findProperties(sourceMap, "orgRegType"));

        return map;

    }

}
